//
//  homePageController.m
//  YouJoin
//
//  Created by MacBookPro on 15/11/9.
//  Copyright © 2015年 MacBookPro. All rights reserved.
//

#import "YJ_homePageController.h"
#import "YJ_StatusFrame.h"
#import "YJ_StatusCell.h"
#import "YJ_Status.h"
#import "AppDelegate.h"
#import "YJ_EditViewCell.h"

@interface YJ_homePageController ()

//用户名
@property (copy,nonatomic) NSString *username;
/**
 *  存放所有cell的frame模型数据
 */
@property (nonatomic, strong) NSArray *statusFrames;

@end

@implementation YJ_homePageController

- (void)viewDidLoad {
    [super viewDidLoad];

////////////////////////////////////////////////////////////////////////////////////////
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
#warning 测试数据
    self.username = @"testing";
    NSString *iconpic = @"tabbar2Sel";
    
    //上中 用户名字
    self.title = self.username;
    
    //左上角用户图片
    UIButton *leftHeadBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 52-INSERT, 33)];
    [leftHeadBtn setImage:[UIImage imageNamed:iconpic] forState:UIControlStateNormal];
    leftHeadBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -INSERT, 0, 0);
    UIBarButtonItem *backItemLeft = [[UIBarButtonItem alloc] initWithCustomView:leftHeadBtn];
    self.navigationItem.leftBarButtonItem = backItemLeft;
    
    //右上 固定操作
    UIButton *rightHeadBtn = [[UIButton alloc]initWithFrame:CGRectMake(41, 2, 30, 30)];
    [rightHeadBtn setImage:[UIImage imageNamed:@"homePage_paopao"] forState:UIControlStateNormal];
    UIBarButtonItem *backItemRight = [[UIBarButtonItem alloc] initWithCustomView:rightHeadBtn];
    self.navigationItem.rightBarButtonItem = backItemRight;
    
    
    NSLog(@"%@",((AppDelegate *)[[UIApplication sharedApplication]delegate]).userInfo);
////////////////////////////////////////////////////////////////////////////////////////
    
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSArray *)statusFrames
{
    if (_statusFrames == nil) {
        // 初始化
        // 1.获得plist的全路径
        NSString *path = [[NSBundle mainBundle] pathForResource:@"statuses.plist" ofType:nil];
        
        // 2.加载数组
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
        
        // 3.将dictArray里面的所有字典转成模型对象,放到新的数组中
        NSMutableArray *statusFrameArray = [NSMutableArray array];
        for (NSDictionary *dict in dictArray) {
            // 3.1.创建YJ_Status模型对象
            YJ_Status *status = [YJ_Status statusWithDict:dict];
            
            // 3.2.创建YJ_StatusFrame模型对象
            YJ_StatusFrame *statusFrame = [[YJ_StatusFrame alloc] init];
            statusFrame.status = status;
            
            // 3.2.添加模型对象到数组中
            [statusFrameArray addObject:statusFrame];
        }
        
        // 4.赋值
        _statusFrames = statusFrameArray;
    }
    return _statusFrames;
}


#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.statusFrames.count+1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //输入栏
    if (indexPath.row == 0) {
        
        // 1.创建cell
        YJ_EditViewCell *cell = [[YJ_EditViewCell alloc]init];
        
        return cell;
        
    }
    
    else{
        // 1.创建cell
        YJ_StatusCell *cell = [YJ_StatusCell cellWithTableView:tableView];
        
        // 2.给cell传递模型
        cell.statusFrame = self.statusFrames[indexPath.row-1];
        
        // 3.返回cell
        return cell;
    }
}

#pragma mark - 实现代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        YJ_EditViewCell *hahaha = [[YJ_EditViewCell alloc]init];
        return hahaha.cellHeight;
    }
    // 取出这行对应的frame模型
    YJ_StatusFrame *statusFrame = self.statusFrames[indexPath.row-1];
    return statusFrame.cellHeight;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
